package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/streadway/amqp"
	"log"
	"time"
	"wskyygyso/casually/Config"
	"wskyygyso/casually/GoftIclass"
	"wskyygyso/casually/Injector"
	"wskyygyso/casually/Lib"
	"wskyygyso/casually/Model"
	"wskyygyso/casually/Services"
	"wskyygyso/casually/WebService"
	"wskyygyso/casually/goft"
)

func test1() {
	start := time.Now().Unix()
	Model.ReadData()
	end := time.Now().Unix()
	fmt.Printf("测试--用时:%d秒\r\n", end-start)
}

func test2() {
	start := time.Now().Unix()
	out := Model.ReadChanData()
	for o := range out {
		fmt.Printf("%d.csv文件执行完成,结果:%v\n", o.Page, o.Err)
	}
	end := time.Now().Unix()
	fmt.Printf("测试--用时:%d秒\r\n", end-start)
}

func test3() {
	start := time.Now().Unix()
	out := Model.Pipe(Model.DataChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan,
		Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan, Model.ReadChan)
	for o := range out {
		fmt.Printf("%d.csv文件执行完成,结果:%v\n", o.Page, o.Err)
	}
	end := time.Now().Unix()
	fmt.Printf("测试--用时:%d秒\r\n", end-start)
}

func test4() {
	start := time.Now().Unix()
	out := Model.Pipe2(Model.DataChan, Model.ReadChan, 50)
	for o := range out {
		fmt.Printf("%d.csv文件执行完成,结果:%v\n", o.Page, o.Err)
	}
	end := time.Now().Unix()
	fmt.Printf("测试--用时:%d秒\r\n", end-start)
}

func injectTest() {
	serviceConfig := Config.NewServiceConfig()
	Injector.BeanFactory.ExprMap = map[string]interface{}{
		"ServiceConfig": serviceConfig,
	}
	Injector.BeanFactory.Set(serviceConfig)
	{
		userService := Services.NewUserService()
		Injector.BeanFactory.Apply(userService)
		fmt.Println(userService.Order)
	}
	{
		adminService := Services.NewAdminService()
		Injector.BeanFactory.Apply(adminService)
		fmt.Println(adminService.Order)
	}
}

func amqpTest() {
	dsn := fmt.Sprintf("amqp://%s:%s@%s:%d/", "guest", "guest", "127.0.0.1", 5672)
	conn, err := amqp.Dial(dsn)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()
}

func userServiceTest() {
	//缓存测试
	WebService.WebService.Handle("GET", "/user/:id", func(context *gin.Context) {
		//从缓存池 取出缓存
		userCache := Lib.UserCache()
		//放回缓存池
		defer Lib.ReleaseUserCache(userCache)
		userId := context.Param("id")
		userCache.DbGetter = Lib.UserDbGetter(userId)
		user := Lib.NewUserModel()
		userCache.GetCacheForObject("userId"+userId, user)
		context.Header("content-type", "application/json")
		context.JSON(200, user)
	})
	WebService.WebService.Run(":8080")
}

func goftTest() {
	goft.Igniter().
		//可以在控制器 同时使用两个ORM
		Beans(goft.NewGormAdapter()). // 实现简单的依赖注入
		//Attach(new(Middleware.UserMiddle)).//带生命周期的中间件
		Mount("v1",
			GoftIclass.NewUserClass(),  //控制器，挂载到v1
			GoftIclass.NewIndexClass(), //控制器，挂载到v1
		).
		Launch()
}

func main() {
	goftTest()
}
